package com.jdew.thkj.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.CouponListBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class SortAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<CouponListBean> dataList;
    private LayoutInflater layoutInflater;
    private View itemView;
    public  Context context;
    private OnItemClickListener mItemClickListener;
    //item类型

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick( (Integer) v.getTag() );
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public SortAdapter(Context context, List<CouponListBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from( context );
    }

    public void setDataList(List<CouponListBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = layoutInflater.inflate( R.layout.product_list_item, parent, false );
        itemView.setOnClickListener( this );
        return new MyViewHolder( itemView );
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
            MyViewHolder myviewHolder = (MyViewHolder) holder;
            myviewHolder.BindView( holder, position );
            myviewHolder.itemView.setTag( position );
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView beforePrice;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.source)
        ImageView source;

        MyViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }

        public void BindView(final RecyclerView.ViewHolder holder, int position) {
            DecimalFormat df = new DecimalFormat( "0.00" );
            Glide.with( context ).load( dataList.get( position ).getThumbnail_pic()).into( product );
            if (dataList.get( position ).getPlatform_id() == 1) {
                source.setImageResource( R.mipmap.tb );
            } else {
                source.setImageResource( R.mipmap.tm );
            }
            ((MyViewHolder) holder).price.setText(df.format(Double.parseDouble(dataList.get( position ).getRaw_price())));
            ((MyViewHolder) holder).price.getPaint().setFlags( Paint.STRIKE_THRU_TEXT_FLAG );
            ((MyViewHolder) holder).price.setText(df.format(Double.parseDouble(dataList.get( position ).getCoupon_price())));
            ((MyViewHolder) holder).num.setText( "销量:" + dataList.get( position ).getMonth_sales());

        }
    }
}
